注意:所有文章除特别说明外,转载请注明出处.
SpringMVC 文件下载
@Controller
public class DownloadController {
@Resource
HttpServletRequest request;
@Resource
HttpServletResponse response;
@RequestMapping("/download")
public String download(@RequestParam String fileName){
//1.设置响应的文件类型和文件编码
response.setContentType("text/html;charset=utf-8");
try {
//2.确保请求的编码类型为UTF-8,不然文件下载后有可能因为类型不一致出现乱码
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
String ctxPath = request.getSession().getServletContext().getRealPath("/")+"upload/";
String downLoadPath = ctxPath + fileName;
System.out.println(downLoadPath);
try{
long fileLength = new File(downLoadPath).length();
//3.设置响应头文件内容,文件类型、弹出下载对话框、文件大小
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
//4.新建一个输入流缓存对象,并将文件输入流对象传递进去,将文件路径传递进文件输入流对象中,这是一个逐步处理的过程
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
//5.新建一个输出流缓存对象,将服务器响应输出流对象至于其中
bos = new BufferedOutputStream(response.getOutputStream());
//6.新建一个缓存
byte[] buff = new byte[2048];
//7.内容字节总数
int bytesRead;
//8.输入到到buff缓存中,当文件为空是read()会return -1,否则返回读取的字节总数
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
//9.将buff的字节写到响应体的输出流中,输出流持续输出到客户端
bos.write(buff, 0, bytesRead);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
try {
//10.关闭缓存输入流对象
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bos != null)
try {
//10.关闭缓存输出流对象
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}